#include "stdafx.h"
#include "CMySql.h"
#include "export.h"

#pragma comment(lib,"MySqlDll.lib")

CMySQL::CMySQL()
{
	m_pszServer	= NULL;
	m_pszUserName	= NULL;
	m_pszPWD	= NULL;
	m_pszDBName	= NULL;
	m_unPort	= 0;
	m_bIsInit	= FALSE;
}

CMySQL::~CMySQL()
{
   CleanUp();
}

BOOL
CMySQL::initMySQL(
    char* pszServer, 
    char* pszUserNsme, 
    char* pszPWD, 
    char* pszDBName, 
    UINT unPort 
    )
{

    BOOL bStatus = FALSE;
    
    do 
    {
        if( m_bIsInit )
        {
           CleanUp();
        }
        if( NULL == pszServer ||
            NULL == pszUserNsme ||
            NULL == pszPWD ||
            NULL == pszDBName ||
            0    == unPort)
        {
            break;
        }

        int length = 0;
        length = strlen( pszServer );
        if( 0 >= length )
        {
            break;
        }
        m_pszServer = new char[length + 1];
        strcpy( m_pszServer, pszServer );

        length = 0;
        length = strlen( pszUserNsme );
        if( 0 >= length )
        {
            break;
        }
        m_pszUserName = new char[length + 1];
        strcpy( m_pszUserName, pszUserNsme );

        length = 0;
        length = strlen( pszPWD );
        if( 0 >= length )
        {
            break;
        }
        m_pszPWD = new char[length + 1];
        strcpy( m_pszPWD, pszPWD );

        length = 0;
        length = strlen( pszDBName );
        if ( 0 >= length )
        {
            break;
        }
       m_pszDBName = new char[length + 1];
       strcpy( m_pszDBName, pszDBName );

       m_unPort = unPort;
       bStatus = TRUE;

    } while( FALSE );

    m_bIsInit = bStatus;

    return bStatus;
}

VOID
CMySQL::CleanUp(
    VOID
    )
{
    if( NULL != m_pszServer )
    {
        delete [] m_pszServer;
        m_pszServer = NULL;
    }

    if( NULL != m_pszUserName )
    {
        delete [] m_pszUserName;
        m_pszUserName = NULL;
    }

    if( NULL != m_pszPWD )
    {
        delete [] m_pszPWD;
        m_pszPWD = NULL;
    }

    if( NULL != m_pszDBName )
    {
        delete [] m_pszDBName;
        m_pszDBName = NULL;
    }

    m_unPort = 0;
    m_bIsInit = FALSE;

}

BOOL
CMySQL::connectTest(
    VOID
    )
{
    return conncetToMysql(  m_MySql,
                            m_pszServer,
                            m_pszUserName,
                            m_pszPWD,
                            m_pszDBName,
                            m_unPort );
}

BOOL
CMySQL::exceteSQL( 
    char* pszSQL 
    )
{
    return TRUE;
}